package com.ey12.service;

import java.util.ArrayList;
import java.util.List;

import com.ey12.db.dao.GmNewsDAO;
import com.ey12.db.domain.GmNews;
import com.ey12.db.domain.GmUser;
import com.ey12.utils.DateUtil;
public class GmNewsService {
	
		private GmNewsDAO gmNewsDAO;

		public GmNewsDAO getGmNewsDAO() {
			return gmNewsDAO;
		}

		public void setGmNewsDAO(GmNewsDAO gmNewsDAO) {
			this.gmNewsDAO = gmNewsDAO;
		}
		public List getHomeList(){
			List result = new ArrayList();
			List list = gmNewsDAO.findHomeList();
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					if(i>9){
						break;
					}
					GmNews gm =(GmNews)list.get(i);
					result.add(gm);
				}
			}
			System.out.println("GmNewsService="+result.size());
			return result;
		}

		public GmNews getNewsDetial(String newsTitle) {
			if (!"".equals(newsTitle)) {
				List list = gmNewsDAO.findByNewsTitle(newsTitle);
				System.out.println(list.size());
				if (list.size() > 0) {
					GmNews news = (GmNews) list.get(0);
					return news;
				}
			}
			return null;
		}
		public List getMoreList(){
			List list = gmNewsDAO.findHomeList();
			System.out.println("GmNewsService="+list.size());
			return list;
		}
		public List getNoticeList(){
			List result = new ArrayList();
			List list = gmNewsDAO.findNoticeList();
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					if(i>8){
						break;
					}
					GmNews gm =(GmNews)list.get(i);
					result.add(gm);
				}
			}
			System.out.println(result.size());
			
			return result;
		}
		public void updateNews(GmNews gmNews) {	
			gmNews.setPublishDate(new DateUtil().toTimeString());
			gmNewsDAO.saveOrUpdate(gmNews);
		}
		public void saveNews(GmNews gmNews) {
			gmNews.setId(null);
			gmNews.setPublishDate(new DateUtil().toTimeString());
			gmNewsDAO.saveOrUpdate(gmNews);
		}
		public void delNews(GmNews gmNews) {
			gmNewsDAO.delete(gmNews);
		}
		public void addNews(String newsTitle,String newsContent) {
			GmNews n = new GmNews();
			n.setNewsTitle(newsTitle);
			n.setNewsContent(newsContent);
			n.setPublishBy("admin");
			n.setPublishDate(new DateUtil().toTimeString());
			gmNewsDAO.saveOrUpdate(n);
		}
		
}

